package com.chinaunicom.credits.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.chinaunicom.credits.model.Gift;
import com.chinaunicom.credits.service.IGiftService;

@Controller
@RequestMapping("/crm/gift")
public class GiftController {
	
	@Resource
	private IGiftService giftService;
	
	@RequestMapping("query")
	public String query(Model model) throws Exception{
			List<Gift> gift = giftService.queryAll();
			model.addAttribute("gift",gift);
			return "/WEB-INF/credits/gift_manage.jsp";
		}
	
	//通过商品类别查询
	@RequestMapping("/queryByType")
	public String queryByType(Model model,HttpServletRequest request) throws Exception{
		String giftType = new String(request.getParameter("giftType").getBytes("iso-8859-1"), "utf-8");
		model.addAttribute("giftType",giftType);
		if(("全部").equals(giftType)){
			List<Gift> gift = giftService.queryAll();
			model.addAttribute("gift",gift);
		}else {
			List<Gift> gift = giftService.queryByType(giftType);
			model.addAttribute("gift",gift);
		}
		return "/WEB-INF/credits/gift_manage.jsp";
		}
	
	//通过商品名称查询
	@RequestMapping("/queryByName")
	public String queryByName(Model model,HttpServletRequest request) throws Exception{
		String giftName = request.getParameter("giftName");
		List<Gift> gift = giftService.queryByName(giftName);
		model.addAttribute("gift",gift);
		return "/WEB-INF/credits/gift_manage.jsp";
		}
	
	@RequestMapping("/addData")
	public String addData(Model model) throws Exception{
		List<String> type = giftService.queryType();
		model.addAttribute("type",type);
		return "/WEB-INF/credits/gift_add.jsp";
		}
	
	@RequestMapping("/save")
	public String save(@RequestParam("vvFile")CommonsMultipartFile upload,Model model,Gift gift) throws Exception{
		String fileName = upload.getOriginalFilename();
 		String newFileName = UUID.randomUUID().toString()+fileName;
 		System.out.println(fileName);
		String Path = "D:/Administrator/Workspaces/MyEclipse 2016/China_Unicom_CRM/src/main/webapp/bootstrap-rs/images/credits/";
		gift.setvFile("bootstrap-rs/images/credits/"+newFileName);
		upload.transferTo(new File(Path+newFileName));
		
		if (gift.getvGiftname() != null && !"".equals(gift.getvGiftname())) {
			giftService.insertSelective(gift);
		}
//		giftService.insertSelective(gift);
		model.addAttribute("gift",gift);
		return "/WEB-INF/credits/gift_add.jsp";
		}
	
	@RequestMapping("detail")
	public String detail(Model model,Short nGiftno) throws Exception{
		Gift giftDetail = giftService.selectByPrimaryKey(nGiftno);
		model.addAttribute("gift",giftDetail);
		return "/WEB-INF/credits/gift_detail.jsp";    
		}
	
	@RequestMapping("updateData")
	public String updateData(Model model,Short nGiftno){
		Gift gift = giftService.selectByPrimaryKey(nGiftno);
		model.addAttribute("gift",gift);
		return "/WEB-INF/credits/gift_update.jsp";
	}
	
	@RequestMapping("update")
	public String update(Model model,Gift gift){
		giftService.updateByPrimaryKeySelective(gift);
		model.addAttribute("gift",gift);
		return "/WEB-INF/credits/gift_update.jsp";
	}
	
	@RequestMapping("delete")
	public String delete(Short nGiftno){
		giftService.deleteByPrimaryKey(nGiftno);
		return "redirect:/crm/gift/query";
	}
	
	@RequestMapping("addCart")
	public String addCart(Short nGiftno,HttpSession session,Model model){
		Gift giftcart = giftService.selectByPrimaryKey(nGiftno);
		List<Gift> gifts = new ArrayList<Gift>();
		gifts.add(giftcart);
		model.addAttribute("giftcart", gifts);
		return "/WEB-INF/credits/gift_cart.jsp";
	}
	
	
}
